package cn.tedu._05mvcboot02.controller;

import cn.tedu._05mvcboot02.base.response.JsonResult;
import cn.tedu._05mvcboot02.mapper.UserMapper;
import cn.tedu._05mvcboot02.pojo.dto.InsertDTO;
import cn.tedu._05mvcboot02.pojo.entity.User;
import cn.tedu._05mvcboot02.pojo.vo.UserListVO;
import cn.tedu._05mvcboot02.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
@Api(tags = "用户模块")
//@Controller
@Slf4j
@RestController
@RequestMapping("/v1/users/")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    /**
     * 1.添加用户
     */
    @ApiOperation(value = "添加用户")
    @PostMapping("insert")
    public JsonResult insert(InsertDTO insertDTO){
        User user = new User();
        BeanUtils.copyProperties(insertDTO,user);
        user.setCreated(new Date());
        userMapper.insert(user);
        return JsonResult.ok();
    }
    /**
     * 2.查询用户列表
     */
    @ApiOperation(value = "查询用户列表")
    @GetMapping("List")
    public JsonResult userList(){
        System.out.println(userMapper.userList());
        List<UserListVO> userListVOS = userMapper.userList();
        return JsonResult.ok(userListVOS);
    }

    /**
     *3.删除用户
     */
    @ApiOperation(value = "删除指定用户")
    @ApiImplicitParam(name = "id",value = "用户编号",required = true,dataType = "int")
    @GetMapping ("delete")
    public JsonResult delete(Integer id){
        userMapper.deleteById(id);
        return JsonResult.ok();
    }
    /**
     * 4.修改用户
     */
    @ApiOperation(value = "更新用户")
    @PostMapping("update")
    public JsonResult update(User user){
        //调用接口，更新指定用户
        userMapper.update(user);
        return JsonResult.ok();
    }
    @ApiOperation(value = "查询指定用户")
    @GetMapping("selectById/{uid}")
    @ApiImplicitParam(name = "uid",value = "用户编号",required = true,dataType = "int")
    public JsonResult selectById(@PathVariable Integer uid){
        log.debug("uid="+uid);
        UserVO userVO = userMapper.selectById(uid);
        return JsonResult.ok(userVO);
    }

}































